package org.jackysoft.file;

import java.io.File;
import java.util.UUID;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jackysoft.entity.User;
import org.jackysoft.util.FileInfo;
import org.jackysoft.util.IOUtils;

public class FileAdapter {
	static final Log log = LogFactory.getLog(FileAdapter.class);
	private byte[] fileData;
	private User owner;
    private String diskBase;
    private String title;
	/**
	 * @param HtmlFile
	 *            hf 文件变量
	 * @param String
	 *            basePath 以<b> \ </b> 结尾
	 * */
	public FileAdapter(byte[] fileData,String title, User owner,String diskBase) {
		this.diskBase = diskBase;
		this.fileData = fileData;
		this.owner = owner;
		this.title = title;
	}

	public FileInfo save() throws java.io.IOException {
		String fs = File.separator;
		String realPath = String.format("%s%s%s", owner.getUsername(),fs,UUID.randomUUID());
		String fullpath =
				String.format("%s:%sfiles%s%s", diskBase,fs,fs,realPath);
		File rfile = new File(fullpath);
		if (rfile.exists())
			rfile.delete();
		else {
		
			IOUtils.saveFile(fileData, rfile);
			log.info("文件 "+rfile.getAbsolutePath()+"  已保存...");
		}
		return new FileInfo(title, realPath);
	}
}
